const {parseToken} = require('../tools/token');

// 请求级的中间件  --- 放在路由处理函数的前面
exports.testLogin = (req , res , next) => {
    // console.log(req.body)
    const {username , password} = req.body ;
    if(!username || !password) {
        return next(1)
    }
    next()
}

// 修改密码的参数判断
exports.testRpwd = (req , res , next) => {
    // console.log(req.body)
    const {newPwd , password , id , token} = req.body ;
    if(!newPwd || !password || !id || !token) {
        return next(1)
    }
    next()
}

// 校验token   ---- 以后别的地方也会需要校验token
exports.testToken =  (req , res , next) => {
    const {token} = req.body ;
    console.log(token);
    // 判断token是否正确
    parseToken(token).then(data => {
        console.log(data) ;
        if(data.code === 0) {
            // res.send(data)
            return next(2);
        }
        next();
    })
}
